package org.qloudgen.borg.pages;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.markup.html.navigation.paging.AjaxPagingNavigator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.UserBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.model.UserModel;

import com.google.common.collect.Lists;

@SuppressWarnings({"unchecked","rawtypes"})
public class UserListPage extends BasePage{

	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */

	static Log log = LogFactory.getLog(UserListPage.class);

	private static final long serialVersionUID = 1L;
	private transient List<UserBean> users = Lists.newArrayList();
	private transient String buttonName;
	
	public UserListPage(final PageParameters pageParameters){
		
		super();
		this.pp = pageParameters;
		
		// get from metadb
		DefaultImpl.getUserList(users);

		// add form 
		Form<?> form = new Form("form",new CompoundPropertyModel<UserModel>(new UserModel())) {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit() {
				
				UserBean user = ((UserModel)getDefaultModelObject()).getGroup();
				buttonName = ((FormComponent<String>)findSubmittingButton()).getInputName();
					
				if (user == null && !buttonName.equals("create")) {
					
					info(CommonValues.INFO_NO_SELECT);
					return;
					
				} 
				
				if (buttonName.equals("create")) {

					setResponsePage(UserCreatePage.class,pageParameters);
					
				} else if (buttonName.equals("pause")) {

					// update user status
					if (user.getUserStatus().equals(CommonValues.STATUS_NORMAL))
						DefaultImpl.updateUserStatus(user.setUserStatus(CommonValues.STATUS_PAUSE));
					else if (user.getUserStatus().equals(CommonValues.STATUS_PAUSE))
						DefaultImpl.updateUserStatus(user.setUserStatus(CommonValues.STATUS_NORMAL));
					else
						info(CommonValues.INFO_CAN_NOT_PAUSED);
					
				} else if (buttonName.equals("right")) {

					// go to right page
					setResponsePage(UserRightsPage.class,new PageParameters().set("userNo", user.getUserNo())
																			 .set("userName", user.getUserName()));
					
				} else if (buttonName.equals("reset")) {

					// reset password
					DefaultImpl.modifyUserPassword(user.setUserPassword(CommonValues.DEFAULT_USER_PASSWORD));
					
				} else {
					
					if (user.getUserNo().equals(CommonValues.USER_TYPE_ADMIN)
					  ||user.getUserNo().equals(CommonValues.USER_TYPE_COMMON)) {
						info(CommonValues.INFO_CAN_NOT_DELETED);
					} else {
						DefaultImpl.deleteUser(user.getUserNo());
						setResponsePage(UserListPage.class,pp );
					}
					
				}
			
			}
			
		};
		this.add(form);
	
		// add group
		final RadioGroup<?> group=new RadioGroup<Object>("group"); 
		group.setOutputMarkupId(true);
		group.setRenderBodyOnly(false);
		form.add(group);
		
		// add cluster list
		final PageableListView<UserBean> listView =new PageableListView<UserBean>( "rows", users, CommonValues.SHOW_LIST_NUMBER) {

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<UserBean> item) {
				final UserBean user = (UserBean) item.getModelObject();
				item.add(new Radio<UserBean>("radio", new Model<UserBean>(user)));
				item.add( new Label( "userNo", user.getUserNo()));
				item.add( new Label( "userName", user.getUserName()));
				item.add( new Label( "userType", CommonValues.userTypeNames.get(user.getUserType())));
				item.add( new Label( "userStatus", CommonValues.statusNames.get(user.getUserStatus())));
				item.add( new Label( "taskNumber", String.valueOf(user.getTaskNumber())));				
			}
		};
		listView.setOutputMarkupId(true);
		group.add(listView);
		
		// add navigator
		final AjaxPagingNavigator nav = new AjaxPagingNavigator("navigator", listView);
		nav.setOutputMarkupId(true);
		group.add(nav);
		
		// add button 
		form.add(new Button("create"));
		form.add(new Button("delete"));	
		form.add(new Button("pause"));	
		form.add(new Button("right"));		
		form.add(new Button("reset"));
	}
}
